package pl.mobilebackup.comm.session;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import pl.mobilebackup.comm.exceptions.MessageNotValidException;

public class ActiveMQTransmitter implements SessionBean {

	private static final long serialVersionUID = 1L;
	
	private SessionContext context = null;
	
//	private RouterLocalHome routerLocalHome;
	
	public void ejbActivate() throws EJBException, RemoteException {
		System.out.println("ActiveMQTransmitter.ejbCreate()");
	}

	public void ejbPassivate() throws EJBException, RemoteException { }

	public void ejbRemove() throws EJBException, RemoteException { }

	public void ejbCreate() throws CreateException {
		System.out.println("ActiveMQTransmitter.ejbCreate()");
		
		try {
			InitialContext context = new InitialContext();
//			Object ejbLocalHome = context.lookup("java:comp/env/Router");
//			routerLocalHome = (RouterLocalHome) PortableRemoteObject.narrow(ejbLocalHome, RouterLocalHome.class);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public void setSessionContext(SessionContext context) throws EJBException, RemoteException {
		this.context = context;
	}

	public SessionContext getSessionContext() {
		return context;
	}
	
	public void routeMessage(final String message) throws MessageNotValidException {
//		Request request = null;
//		try {
//			request = (Request) Request.deserialize(message);
//			
//			RouterLocal routerLocal = routerLocalHome.create();
//			routerLocal.route(request);
//		} catch (DeserializationException e) {
//			throw new MessageNotValidException();
//		} catch (CreateException e) {
//			e.printStackTrace();
//		}
	}
	
//	public void routeFile(final FileMessage file) {
//		try {
//			RouterLocal routerLocal = routerLocalHome.create();
//			routerLocal.route(file);
//		} catch (CreateException e) {
//			e.printStackTrace();
//		}
//	}
}
